package com.ys.service.bi.impl;


import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.ys.entity.bi.BiFinancialFunds;
import com.ys.mapper.bi.BiMapper;
import com.ys.service.bi.BiFinancialFundsService;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 财务BI
 */
@Service("biFinancialFundsService")
public class BiFinancialFundsServiceImpl implements BiFinancialFundsService {
	private static final Logger logger = LoggerFactory.getLogger(BiFinancialFundsServiceImpl.class);


	@Autowired
	private BiMapper biMapper;


	/**
	 * 每天定时新增财务资金
	 */
	@Override
	public R addByFinancialFunds() throws Exception{
		List<BiFinancialFunds> biFinancialFundsList = new ArrayList<>();
		//万里汇账户新增
		List<BiFinancialFunds> cwBiFinancialFundsList = biMapper.getCwBiFinancialFundsList();
		for (BiFinancialFunds biFinancialFunds :cwBiFinancialFundsList){
			biFinancialFundsList.add(biFinancialFunds);
		}
		//对公户新增
		List<BiFinancialFunds> cwTcwFinancialTransactionFlow = biMapper.getCwTcwFinancialTransactionFlow();

		//进行排序
		for (BiFinancialFunds biFinancialFunds :cwTcwFinancialTransactionFlow){
			//逸思科技 C101
			if (biFinancialFunds.getVid().equals("C101")){
				biFinancialFunds.setFinancialFundsSort(2);
			}else if (biFinancialFunds.getVid().equals("C103")){
				//福州逸博供应链有限公司 C103
				biFinancialFunds.setFinancialFundsSort(3);
			}else if (biFinancialFunds.getVid().equals("C104")){
				//福州福沃洋国际商贸有限公司 C104
				biFinancialFunds.setFinancialFundsSort(4);
			}else if (biFinancialFunds.getVid().equals("C102")){
				//福州逸思电子商务有限公司 C102
				biFinancialFunds.setFinancialFundsSort(5);
			}
			biFinancialFundsList.add(biFinancialFunds);
		}

		//预支账户新增
		List<BiFinancialFunds> cwAdvanceAccount = biMapper.getCwAdvanceAccount();
		for (BiFinancialFunds biFinancialFunds :cwAdvanceAccount){
			biFinancialFundsList.add(biFinancialFunds);
		}

		//批量新增
		int biFinancialFundsListLength = biFinancialFundsList.size();
		int j = 0;
		while (biFinancialFundsListLength > 50) {
			biMapper.insetBatchBiFinancialFunds(biFinancialFundsList.subList(j, j + 50));
			j = j + 50;
			biFinancialFundsListLength = biFinancialFundsListLength - 50;
		}
		if (biFinancialFundsListLength > 0) {
			biMapper.insetBatchBiFinancialFunds(biFinancialFundsList.subList(j, j + biFinancialFundsListLength));
		}
		return R.ok();
	}

	//bi页面获取数据，根据类型
	@Override
	public JSONObject getBiFinancialFundsJson() {
		JSONObject res = new JSONObject();
		//查询财务资金数据
		List<Map<String,Object>> biFinancialFundsList = new BiFinancialFunds()
				.addField("CASE WHEN A.AccountType = 0 THEN '对公账户' WHEN A.AccountType = 1 THEN '万里汇账户' ELSE '预支账户' END accountTypeName")
				.addField("ISNULL(A.remark,'') AS  remark1")
				.addField("FORMAT(A.UpdateTime, 'yyyy-MM-dd hh:mm:ss') AS  updateTime1")
				.where("A.WriteTime = CONVERT ( VARCHAR ( 10 ), GETDATE(), 120 )")
				.setOrderby("A.financialFundsSort ASC,A.vname ASC")
				.queryMap();
		res.put("biFinancialFundsList",biFinancialFundsList);
		String updateTime1 = "未知";
		if (biFinancialFundsList.size() != 0){
			updateTime1 = "("+biFinancialFundsList.get(biFinancialFundsList.size()-1).get("updateTime1")+")";
		}
		res.put("updateTime1",updateTime1);
		//金额
		BigDecimal cny = BigDecimal.ZERO;
		BigDecimal cnh = BigDecimal.ZERO;
		BigDecimal usd = BigDecimal.ZERO;
		BigDecimal eur = BigDecimal.ZERO;
		BigDecimal gbp = BigDecimal.ZERO;
		BigDecimal other = BigDecimal.ZERO;
		for (Map<String,Object> biFinancialFunds :biFinancialFundsList){
			//在岸人民币
			if (biFinancialFunds.get("accountType").toString().equals("1")){
				if (biFinancialFunds.get("currencyCode").toString().equals("CNH")){
					//离岸人民币
					cnh = new BigDecimal(biFinancialFunds.get("amount").toString());
				}else if (biFinancialFunds.get("currencyCode").toString().equals("USD")){
					//美元
					usd = new BigDecimal(biFinancialFunds.get("amount").toString());
				} else if (biFinancialFunds.get("currencyCode").toString().equals("EUR")){
					//欧元
					eur = new BigDecimal(biFinancialFunds.get("amount").toString());
				} else if (biFinancialFunds.get("currencyCode").toString().equals("GBP")){
					//英镑
					gbp = new BigDecimal(biFinancialFunds.get("amount").toString());
				}else {
					//其它
					other = new BigDecimal(biFinancialFunds.get("amount").toString());
				}
			}else {
				cny = cny.add(new BigDecimal(biFinancialFunds.get("amount").toString()));
			}
		}
		res.put("cny",cny);
		res.put("cnh",cnh);
		res.put("usd",usd);
		res.put("eur",eur);
		res.put("gbp",gbp);
		res.put("other",other);
		return res;
	}


	/**
	 * 获取财务资金趋势
	 * @return
	 */
	@Override
	public JSONObject getBiFinancialTrend() {
		JSONObject res = new JSONObject();
		JSONObject dataRmb = new JSONObject();
		JSONObject dataYb = new JSONObject();
		JSONObject dataUsd = new JSONObject();
		JSONObject dataEur = new JSONObject();
		JSONObject dataGbp = new JSONObject();
		List<String> timeList = new ArrayList<>();
		//获取近12个月份最后一天
		LocalDate today = LocalDate.now();
		for (int i = 11; i > 0; i--) {
			LocalDate lastDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.lastDayOfMonth());
			timeList.add(lastDayOfMonth.toString());
		}
		timeList.add(today.minusDays(0).toString());
		List<BiFinancialFunds> biFinancialFundsList = biMapper.getBiFinancialTrend(timeList);
		for (String time : timeList){
			for (BiFinancialFunds biFinancialFunds : biFinancialFundsList){
				if (time.equals(biFinancialFunds.getWriteTime())){
					//预支+对公 RMB金额合计
					if (biFinancialFunds.getAccountType() != 1){
						if (dataRmb.get(time) != null){
							dataRmb.put(time,new BigDecimal(dataRmb.get(time).toString()).add(biFinancialFunds.getAmount()));
						}else {
							dataRmb.put(time,biFinancialFunds.getAmount());
						}
					}else {
						//CNH
						if (biFinancialFunds.getCurrencyCode().equals("CNH")){
							if (dataYb.get(time) != null){
								dataYb.put(time,new BigDecimal(dataYb.get(time).toString()).add(biFinancialFunds.getAmount()));
							}else {
								dataYb.put(time,biFinancialFunds.getAmount());
							}
						}
						//USD
						if (biFinancialFunds.getCurrencyCode().equals("USD")){
							if (dataUsd.get(time) != null){
								dataUsd.put(time,new BigDecimal(dataUsd.get(time).toString()).add(biFinancialFunds.getAmount()));
							}else {
								dataUsd.put(time,biFinancialFunds.getAmount());
							}
						}
						//EUR
						if (biFinancialFunds.getCurrencyCode().equals("EUR")){
							if (dataEur.get(time) != null){
								dataEur.put(time,new BigDecimal(dataEur.get(time).toString()).add(biFinancialFunds.getAmount()));
							}else {
								dataEur.put(time,biFinancialFunds.getAmount());
							}
						}
						//GBP
						if (biFinancialFunds.getCurrencyCode().equals("GBP")){
							if (dataGbp.get(time) != null){
								dataGbp.put(time,new BigDecimal(dataGbp.get(time).toString()).add(biFinancialFunds.getAmount()));
							}else {
								dataGbp.put(time,biFinancialFunds.getAmount());
							}
						}
					}

				}
			}
		}
		//对应值封装
		List<BigDecimal> cnyList = new ArrayList<>();
		List<BigDecimal> cnhList = new ArrayList<>();
		List<BigDecimal> usdList = new ArrayList<>();
		List<BigDecimal> eurList = new ArrayList<>();
		List<BigDecimal> gbpList = new ArrayList<>();
		for (String time : timeList){
			if (dataRmb.get(time) == null){
				cnyList.add(BigDecimal.ZERO);
			}else {
				cnyList.add(new BigDecimal(dataRmb.get(time).toString()));
			}
			if (dataYb.get(time) == null){
				cnhList.add(BigDecimal.ZERO);
			}else {
				cnhList.add(new BigDecimal(dataYb.get(time).toString()));
			}
			if (dataUsd.get(time) == null){
				usdList.add(BigDecimal.ZERO);
			}else {
				usdList.add(new BigDecimal(dataUsd.get(time).toString()));
			}
			if (dataEur.get(time) == null){
				eurList.add(BigDecimal.ZERO);
			}else {
				eurList.add(new BigDecimal(dataEur.get(time).toString()));
			}
			if (dataGbp.get(time) == null){
				gbpList.add(BigDecimal.ZERO);
			}else {
				gbpList.add(new BigDecimal(dataGbp.get(time).toString()));
			}
		}
		res.put("timeList",timeList);
		res.put("cnyList",cnyList);
		res.put("cnhList",cnhList);
		res.put("usdList",usdList);
		res.put("eurList",eurList);
		res.put("gbpList",gbpList);
		String updateTime1 = "未知";
		//获取最新的刷新时间
		Map<String, Object> objectMap = new BiFinancialFunds().addField("FORMAT(A.UpdateTime, 'yyyy-MM-dd hh:mm:ss') AS  updateTime1").setOrderby("A.writeTime DESC").queryFirstMap();
		if (objectMap.size() != 0){
			updateTime1 = "("+objectMap.get("updateTime1")+")";
		}
		res.put("updateTime1",updateTime1);
		return res;
	}
}
